package cn.wolfcode.crm.web.controller;


import cn.wolfcode.crm.domain.Customer;
import cn.wolfcode.crm.domain.Employee;

import cn.wolfcode.crm.query.CustomerQueryObject;
import cn.wolfcode.crm.query.JsonResult;
import cn.wolfcode.crm.service.ICustomerService;
import cn.wolfcode.crm.service.IEmployeeService;
import cn.wolfcode.crm.service.ISystemDictionaryItemService;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    private ICustomerService customerService;

    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;

    @Autowired
    private IEmployeeService employeeService;


    @RequiresPermissions(value = {"customer:potentialList","潜在客户页面"},logical = Logical.OR)
    @RequestMapping("/potentialList")
    public String potentialList(Model model, @ModelAttribute("qo") CustomerQueryObject qo){
        // 设置为潜在客户
        qo.setStatus(Customer.STATUS_COMMON);
        // 判断是否为管理员或经理,如果不是 销售员只能看到自己的客户
        Subject subject = SecurityUtils.getSubject();
        Employee employee  = (Employee) SecurityUtils.getSubject().getPrincipal();
        System.out.println("获取的用户id:"+employee.getId());
        if (!subject.hasRole("ADMIN") || subject.hasRole("Market_Manager")) {
            qo.setSellerId(employee.getId());
        }
        System.out.println("销售id:"+qo.getSellerId());
        PageInfo<Customer> pageInfo = customerService.query(qo);
        String[] array = {"Market","Market_Manager"};
        // 下拉框职业
        model.addAttribute("jobs",systemDictionaryItemService.selectByJob("job"));
        // 下拉框销售人员
        model.addAttribute("sellers",employeeService.selectSellers(array));
        // 下拉框来源
        model.addAttribute("sources",systemDictionaryItemService.selectBySources("source"));
        model.addAttribute("ccts",systemDictionaryItemService.selectByselectByCommuniCationMethod("communicationMethod"));
        model.addAttribute("result", pageInfo);
        return "customer/potentialList";
    }

    @RequiresPermissions(value = {"customer:list","客户页面"},logical = Logical.OR)
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") CustomerQueryObject qo){
        PageInfo<Customer> pageInfo = customerService.query(qo);
        model.addAttribute("result", pageInfo);
        return "customer/list";
    }

    @RequestMapping("/delete")
    @RequiresPermissions(value = {"customer:delete","客户删除"},logical = Logical.OR)
    @ResponseBody
    public JsonResult delete(Long id){
        if (id != null) {
            customerService.delete(id);
        }
        return new JsonResult(true,"删除成功");
    }


    @RequestMapping("/saveOrUpdate")
    @RequiresPermissions(value = {"customer:saveOrUpdate","客户新增/编辑"},logical = Logical.OR)
    @ResponseBody
    public JsonResult saveOrUpdate(Customer customer){
        if (customer.getId() != null) {
            customerService.update(customer);
            return new JsonResult(true,"修改成功");
        }else {
            customerService.save(customer);
        }
        return new JsonResult(true,"添加成功");
    }
}
